<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item label="医院名称">
        <el-input v-model="searchObj.hosname" placeholder="医院名称"></el-input>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="searchObj.hoscode" placeholder="医院编号"></el-input>
      </el-form-item>
      <el-button type="primary" @click="query()">查询</el-button>
      <el-button type="primary" @click="empty()">清空</el-button>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="hosname" label="医院名称" width="180" />

      <el-table-column prop="hoscode" label="医院编号" width="140" />

      <el-table-column prop="apiUrl" label="地址" width="200" />

      <el-table-column prop="contactsName" label="联系人" width="180"/>

      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <router-link :to="'/yygh/hospset/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="primary"
            size="mini"
            icon="el-icon-delete"
            @click="lockHostSet(scope.row.id, 0)"
            >锁定</el-button
          >
          <el-button
            v-if="scope.row.status == 0"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="lockHostSet(scope.row.id, 1)"
            >取消锁定</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" @click="batchDelete()">批量删除</el-button>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import hospset from "@/api/hospset.js";
export default {
  data() {
    return {
      // 变量
      list: [],
      total: 0,
      searchObj: {
        hosname: "",
        hoscode: ""
      },
      listLoading: false,
      page: 1,
      limit: 3,
      selection: []
    };
  },
  methods: {
    lockHostSet(id, status) {
      // 调用后端接口
      hospset.udpateStatus(id, status).then(resp => {
        // 刷新数据
        this.getPageInfo()
      })
    },
    batchDelete() {
      this.$confirm("此操作将永久删除这些数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 定义一个ids
          let ids = [];
          // 遍历数据id加入数组中
          for (let i = 0; i < this.selection.length; i++) {
            let obj = this.selection[i];
            let id = obj.id;
            ids.push(id);
          }
          // 向后端发送请求
          hospset.batchDelete(ids).then(resp => {
            // 成功的回调
            this.$message({
              type: "success",
              message: "批量删除成功!"
            });
            this.getPageInfo();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSelectionChange(selection) {
      // selection获取到的是选中的记录数据
      this.selection = selection;
      // console.log(selection);
    },
    empty() {
      this.searchObj = {};
      // 清空后再查询一次
      this.getPageInfo();
    },
    query() {
      console.log("query");
      this.getPageInfo();
    },
    handleCurrentChange(val) {
      this.getPageInfo(val);
    },
    getPageInfo(val = 1) {
      // 默认查询第一页
      this.page = val;
      this.listLoading = true;
      hospset
        .getHospsetPage(this.page, this.limit, this.searchObj)
        .then(resp => {
          this.total = resp.data.total;
          this.list = resp.data.rows;
        });
      this.listLoading = false;
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 成功回调才调用删除方法
          hospset.removeDataById(id).then(resp => {
            this.$message({
              type: "success",
              message: "删除成功"
            });
            // 重新查询一次
            this.getPageInfo();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {
    this.getPageInfo();
  },
  mounted() {}
};
</script>

<style></style>
